﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace DbSharp.Utils
{
    public class ProcessLauncher
    {

        public static void RunConsoleApp(string consoleApp, string argument, out int exitCode, out string outputMsg)
        {
            outputMsg = string.Empty;
            try
            {
                Process process = new Process();
                process.StartInfo.FileName = consoleApp;
                process.StartInfo.Arguments = argument;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardInput = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                process.Start();
                process.WaitForExit();
                exitCode = process.ExitCode;
                outputMsg = process.StandardOutput.ReadToEnd();
            }
            catch (Exception ex)
            {
                exitCode = -10000;
                outputMsg = outputMsg + ex.ToString();
            }
        }

        public static void RunDosCmd(string command, out int exitCode, out string outputMsg)
        {
            RunConsoleApp("cmd.exe", "/c " + command, out exitCode, out outputMsg);
        }

        public static void CallWindowsApp(string appName, bool waitForExit)
        {
            try
            {
                Process proc = new Process();
                proc.EnableRaisingEvents = false;
                //call calculator
                //proc.StartInfo.FileName = "calc";
                proc.StartInfo.FileName = appName;
                proc.Start();
                if (waitForExit)
                    proc.WaitForExit();
            }
            catch { }
        }

        public static void GotoWebSiteByIE(string url, bool waitForExit)
        {
            try
            {
                Process proc = new Process();
                proc.EnableRaisingEvents = false;
                proc.StartInfo.FileName = "iexplore";
                //proc.StartInfo.Arguments=@"http://www.microsoft.com";
                proc.StartInfo.Arguments = url;
                proc.Start();
                if (waitForExit)
                    proc.WaitForExit();
            }
            catch { }
        }

        public static void OpenFileByOS(string fileName, bool waitForExit)
        {
            try
            {
                string localUrl;
                if (File.Exists(fileName))
                {
                    UriBuilder uri = new UriBuilder();
                    uri.Scheme = "file";
                    uri.Host = "localhost";
                    uri.Path = fileName;

                    localUrl = uri.ToString();
                    var proc = Process.Start(fileName);
                    if (waitForExit)
                        proc.WaitForExit();
                }
            }
            catch { }
        }


        public static void OpenFileByApp(string appName, string fileName, bool waitForExit)
        {
            try
            {
                Process proc = new Process();
                proc.EnableRaisingEvents = false;
                //proc.StartInfo.FileName = "iexplore";
                proc.StartInfo.FileName = appName;

                //proc.StartInfo.Arguments=@"http://www.microsoft.com";
                proc.StartInfo.Arguments = fileName;
                proc.Start();
                if (waitForExit)
                    proc.WaitForExit();
            }
            catch { }
        }


        public static void OpenWordDocument(string docFileName, bool waitForExit)
        {
            try
            {
                Process proc = new Process();
                proc.EnableRaisingEvents = false;
                proc.StartInfo.FileName = "winword";
                proc.StartInfo.Arguments = docFileName;
                proc.Start();
                if (waitForExit)
                    proc.WaitForExit();
            }
            catch { }
        }

        public static void ExecuteBatchFile(string batFileName, bool waitForExit)
        {
            try
            {
                Process proc = new Process();
                proc.EnableRaisingEvents = false;
                //proc.StartInfo.FileName = "c:\\dotnetstuff\\netdrv.bat";
                proc.StartInfo.FileName = batFileName;
                proc.Start();
                //MessageBox.Show("Map Drive Created");
                if (waitForExit)
                    proc.WaitForExit();
            }
            catch { }
        } 

    }
}
